import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

public class WuxingCode {

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator generator = new AutoGenerator();
        // 全局配置
        GlobalConfig config = new GlobalConfig();
        String s = System.getProperty("user.dir");
        //TODO
        config.setOutputDir(s+"/EasyTrans/src/main/java");
        config.setAuthor("");
        config.setOpen(false);
        config.setFileOverride(false);
        config.setServiceName("%sService");
        //TODO
        config.setIdType(IdType.ASSIGN_ID);          //注意这个
        config.setDateType(DateType.TIME_PACK);
        config.setBaseResultMap(true);//生成基本的resultMap
        config.setBaseColumnList(true);//生成基本的SQL片段
        config.setEntityName("%sDO");
        //实体属性 Swagger2 注解
        config.setSwagger2(true);
        generator.setGlobalConfig(config);
        // 数据源配置
        DataSourceConfig config1 = new DataSourceConfig();
        //TODO
        config1.setUrl("jdbc:mysql://localhost:3306/easyTrans?useSSL=false&useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
//        config1.setUrl("jdbc:mysql://rm-2vc7l62g4u9rt2l82vo.mysql.cn-chengdu.rds.aliyuncs.com:3306/materialbasecloud?useSSL=false&useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
        config1.setDriverName("com.mysql.cj.jdbc.Driver");
        config1.setUsername("root");
        config1.setPassword("123456");
//        config1.setPassword("asd34as25@@");
        config1.setDbType(DbType.MYSQL);
        generator.setDataSource(config1);
        //包配置
        PackageConfig config2 = new PackageConfig();
        config2.setModuleName("");
        config2.setParent("com.wuxing");
        config2.setEntity("model");
        config2.setMapper("mapper");
        config2.setService("service");
        config2.setController("controller");
        generator.setPackageInfo(config2);

        //策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("order");     //设置要映射的表

        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setTablePrefix("duli_");
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);

        strategy.setLogicDeleteFieldName("is_deleted");
        TableFill create = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill modified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        List<TableFill> list = new ArrayList<>();
        list.add(create);
        list.add(modified);
        strategy.setTableFillList(list);
        //乐观锁
        strategy.setVersionFieldName("version");

        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        generator.setStrategy(strategy);
        generator.execute();//执行
    }
}